package org.bookie.mtest;


import static org.testng.Assert.assertEquals;

import java.io.IOException;
import java.net.MalformedURLException;

import org.bookie.mtest.framework.Application;
import org.bookie.mtest.framework.Form;
import org.bookie.mtest.framework.Page;
import org.bookie.mtest.framework.ResultsPage;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import com.meterware.httpunit.WebTable;

@Test
public class ResultsPageTest {
	
	private static final String CATEGORY_SELECTION = "categorySelection";
	private static final String END_DATE_PICKER = "endDatePicker";
	
	public void testResultsTable() throws Exception {
		testResultsTable(createPage(), 2+1, 4);
	}
	
	public void testPeriodForm() throws Exception {
		ResultsPage page = createPage();
		Form form = page.getPeriodForm();
		form.assertHasParameterNamed("startDatePicker");
		form.assertHasParameterNamed(END_DATE_PICKER);
		form.setField(END_DATE_PICKER, "1 Jan 1970");
		testResultsTable(page, 3, 4);
		form.submit("addPeriod");
		testResultsTable(page, 2, 4);
	}
	
	public void testCategorySelection() throws Exception {
		ResultsPage page = createPage();
		Form form = page.getCategoryForm();
		form.assertHasParameterNamed(CATEGORY_SELECTION);
		form.setField(CATEGORY_SELECTION, "2");
		testResultsTable(page, 3, 4);
		form.submit();
		testResultsTable(page, 2, 4);
	}
	
	private void testResultsTable(ResultsPage page, int row, int column) throws SAXException{
		WebTable table = page.getResultsTable();
		assertEquals(table.getColumnCount(), column);
		assertEquals(table.getRowCount(), row);
	}
	
	private ResultsPage createPage() throws MalformedURLException, IOException, SAXException{
		Application application = new Application();
		Page home = application.start();
		ResultsPage results = home.getResultsPage();
		results.assertPageTitle("Event Results");
		return results;
	}
}
